import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import path from 'path'
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'

// https://vitejs.dev/config/
export default defineConfig(({ mode }) => {
  const env = loadEnv(mode, process.cwd(), '')
  return {
    plugins: [
      vue(),
      createSvgIconsPlugin({
        // 指定要缓存的图标文件夹
        iconDirs: [path.resolve(process.cwd(), 'src/assets/svg')],
        // 执行icon name的格式
        symbolId: 'icon-[name]',
      }),
      AutoImport({
        resolvers: [
          ElementPlusResolver({
            importStyle: 'sass',
          }),
        ],
      }),
      Components({
        resolvers: [
          ElementPlusResolver({
            importStyle: 'sass',
          }),
        ],
      }),
    ],
    resolve: {
      alias: {
        '@': path.resolve('./src'), // @代替src
      },
    },
    base: env.VITE_APP_BASE,
    server: {
      host: '0.0.0.0',
      open: true, // 启动项目自动弹出浏览器
      port: env.VITE_PORT || 4000, // 启动端口
      proxy: {
        '/api': {
          // target: 'http://192.168.3.7:10031', // 罗显文
          target: env.VITE_APP_BASE_URL || 'http://192.168.3.2:10031', // 田锐
          // target: 'http://192.168.3.37:10031', // 周琦然
          // target: 'http://192.168.3.44:10031', // 李良超
          // target: 'http://47.104.69.102:10020/caijiaapi',
          // target: 'http://139.196.163.222:8090/caijiaapi',
          // target: 'https://zhyq.chinasilian.com.cn:10010/caijiaapi',
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/api/, ''),
        },
        '/map': {
          target: 'http://139.196.163.222:8090',
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/map/, ''),
        },
        '/video': {
          // target: 'http://192.168.3.7:10031', // 罗显文
          // target: env.VITE_APP_BASE_URL || 'http://192.168.3.2:10031', // 田锐
          // target: 'http://192.168.3.37:10031', // 周琦然
          // target: 'http://192.168.3.44:10031', // 李良超
          // target: 'http://47.104.69.102:10020/caijiaapi',
          // target: 'http://139.196.163.222:8090/caijiaapi',
          target: 'https://zhyq.chinasilian.com.cn:10010/video',
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/video/, ''),
        },
      },
    },
  }
})
